<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class detail_disease extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('medical_disease_model');
        $this->load->model('medical_parts_model');
        $this->load->model('medical_department_model');
        $this->load->model('medical_symptoms_model');
        $this->load->model('medical_object_model');
        $this->load->model('cms_tag_model');
        $this->load->model('cms_category_model');
        $this->load->model('cms_article_model');
        $this->load->model('admin_model');
    }

    protected function _eventLoadLayout() {
        $this->page['title'] = 'Giải pháp y tế cộng đồng';
        $this->module['title'] = 'Giải pháp y tế cộng đồng';
    }

    function index($id = '') {
        $data = array();
        try{
            $disease_detail = $this->medical_disease_model->load_by_id($id);

            if(!isset($disease_detail->id)){
                redirect(site_url('index', true));
            }

            //lây danh mục của bệnh (khoa)
            $dep_detail       = $this->medical_department_model->load_by_id($disease_detail->department_id);
            $dep_detail->href = BASE_URL . renderToAlias($dep_detail->name) . '-dp'.$dep_detail->id . '.html';
            $data['dep_detail'] = $dep_detail;

            //danh mục bệnh là khoa luôn
            $result_dep = $this->medical_disease_model->db->query("SELECT * FROM medical_department WHERE is_hidden = 1");
            $list_dep   = array();
            foreach ($result_dep->result() as $row) {
                $row->href          = BASE_URL . renderToAlias($row->name) . '-dp'.$row->id . '.html';
                $list_dep[$row->id] = $row;
            }
            $data['menu_medical'] = $list_dep;

            //danh mục tin tức
            $result_cat = $this->medical_disease_model->db->query("SELECT * FROM cms_category WHERE is_active = 1");
            $list_cat   = array();
            foreach ($result_cat->result() as $row) {
                $row->href          = BASE_URL . renderToAlias($row->name) . '-ca'.$row->id . '.html';
                $list_cat[$row->id] = $row;
            }
            $data['menu_news'] = $list_cat;

            //triệu chứng liên quan
            if(isset($disease_detail->symptoms_ids) && $disease_detail->symptoms_ids){
                $symptom_relate = $this->medical_symptoms_model->get_by_ids($disease_detail->symptoms_ids);
                $symptom        = array();
                foreach ($symptom_relate as $row) {
                    $row->image        = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->href         = BASE_URL . 'trieu-chung/' . renderToAlias($row->name) . '-st'.$row->id . '.html';
                    $symptom[$row->id] = $row;
                }

                $data['symptom_relate'] = $symptom;
            }

            //bệnh liên quan
            if(isset($disease_detail->disease_ids) && $disease_detail->disease_ids){
                $disease_relate = $this->medical_disease_model->get_by_ids($disease_detail->disease_ids);
                $disease_r      = array();
                foreach ($disease_relate as $row) {
                    $row->image        = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->href         = BASE_URL . 'benh/' . $row->alias . '-ds'.$row->id . '.html';
                    $disease_r[$row->id] = $row;
                }

                $data['disease_relate'] = $disease_r;
            }

            //bệnh cùng khoa bạn nên xem (coi như danh mục của bẹnh)
            if(isset($disease_detail->department_id) && $disease_detail->department_id){
                $res_dis_dep = $this->medical_disease_model->db->query('SELECT id, name, alias, image, short_description FROM medical_disease WHERE department_id = '. $disease_detail->department_id .' AND is_hidden = 1 ORDER BY id DESC LIMIT 4');
                $disease_dep = array();

                foreach ($res_dis_dep->result() as $row) {
                    $row->image        = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->href         = BASE_URL . 'benh/' . $row->alias . '-ds'.$row->id . '.html';
                    $row->date         = (isset($row->update_date)) ? date("d-m-Y", $row->update_date) : date('d-m-Y', time());
                    $disease_dep[$row->id] = $row;
                }

                $data['disease_dep'] = $disease_dep;
            }

            //tin tức tieu diem (lấy những thằng hot_article mới nhất)
            $query_article = 'SELECT id, title, image, description, updated_at  FROM cms_article WHERE hot_article = 1 AND status = 1 ORDER BY id DESC LIMIT 4';
            $res_article   = $this->medical_disease_model->db->query($query_article);
            $hot_article   = array();
            if($res_article){
                foreach ($res_article->result() as $row) {
                    $row->image = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->date  = date('d-m-Y', $row->updated_at);
                    $hot_article[$row->id] = $row;
                }
            }

            //tin tức mới nhất
            $query_news   = 'SELECT id, title, image, description, updated_at  FROM cms_article WHERE status = 1 ORDER BY id DESC LIMIT 4';
            $res_news     = $this->medical_disease_model->db->query($query_news);
            $news_article = array();
            if($res_news){
                foreach ($res_news->result() as $row) {
                    $row->image = ($row->image) ? render_src($row->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                    $row->date  = date('d-m-Y', $row->updated_at);
                    $news_article[$row->id] = $row;
                }
            }

            //tin tức liên quan
            if(isset($disease_detail->category_article_ids) && $disease_detail->category_article_ids){//danh sách category_article_ids dùng để check khi edit
                $temp_group = json_decode($disease_detail->category_article_ids, true);
                $list_group = array();
                foreach($temp_group as $val_g){
                    $art_detail = $this->cms_article_model->load_by_id($val_g['article_id']);

                    if(isset($art_detail->id)){
                        $art_detail->image = ($art_detail->image) ? render_src($art_detail->image) : BASE_URL. "themes/standard/images/top_test.jpg";
                        $list_group[$val_g['article_id']] = $art_detail;
                    }
                }

                $data['list_group']    = $list_group;
            }


            $disease_detail->create_date = (isset($disease_detail->created_date) && $disease_detail->created_date) ? date('d-m-Y', $disease_detail->created_date) : date('d-m-Y', time());
            if(isset($disease_detail->user_id) && $disease_detail->user_id){
                $user = $this->admin_model->load_by_id($disease_detail->user_id);
                $disease_detail->username = $user->name;
            }else{
                $disease_detail->username = 'Admin';
            }

            $data['hot_article']    = $hot_article;
            $data['news_article']   = $news_article;
            $data['disease_detail'] = $disease_detail;
            $this->setPageContent('home/detail_disease', $data);
            $this->renderLayout('layouts/layout_detail');
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

}